﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Drawing.Printing;
using System.Xml;
using System.IO;

namespace NursingFormSystem
{
    /// <summary>
    /// 体温信息文档对象
    /// </summary>
    [Serializable]
    [System.Runtime.InteropServices.ComVisible(false)]
    public class TemperatureDocument
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public TemperatureDocument()
        {

        }

        private string _Info = null;
        /// <summary>
        /// 数据集合-开放给手机端 Date1&2017-08-27@0¤
        /// </summary>
        [DefaultValue(null)]
        public string Info
        {
            get { return _Info; }
            set { _Info = value; }
        }

        private string _Blank_Lines = null;
        /// <summary>
        /// 数据集合-开放存储自定义空行 格式：页码,项目ID,项目名称;
        /// </summary>
        [DefaultValue(null)]
        public string Blank_Lines
        {
            get { return _Blank_Lines; }
            set { _Blank_Lines = value; }
        }

        private string _AddTimeHour = "";
        /// <summary>
        /// 存放当前添加的时间点--固定时间节点之外加测
        /// </summary>
        [DefaultValue("")]
        public string AddTimeHour
        {
            get { return _AddTimeHour; }
            set { _AddTimeHour = value; }
        }

        private string _Title = null;
        /// <summary>
        /// 标题
        /// </summary>
        [DefaultValue(null)]
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }

        private string _PatientID = null;
        /// <summary>
        /// 住院号
        /// </summary>
        [DefaultValue(null)]
        public string PatientID
        {
            get { return _PatientID; }
            set { _PatientID = value; }
        }

        private string _PatientName = null;
        /// <summary>
        /// 病人姓名
        /// </summary>
        [DefaultValue(null)]
        public string PatientName
        {
            get { return _PatientName; }
            set { _PatientName = value; }
        }

        private string _PatientAge = null;
        /// <summary>
        /// 病人年龄
        /// </summary>
        [DefaultValue(null)]
        public string PatientAge
        {
            get { return _PatientAge; }
            set { _PatientAge = value; }
        }

        private string _PatientSex = null;
        /// <summary>
        /// 病人性别
        /// </summary>
        [DefaultValue(null)]
        public string PatientSex
        {
            get { return _PatientSex; }
            set { _PatientSex = value; }
        }

        private string _Section = null;
        /// <summary>
        /// 病区号
        /// </summary>
        [DefaultValue(null)]
        public string Section
        {
            get { return _Section; }
            set { _Section = value; }
        }

        private string _TransferSection = "";
        /// <summary>
        /// 转科前病区/科室(拼接) N16,N7;N7,N18; 逗号分割当次转科前后床号
        /// </summary>
        [DefaultValue("")]
        public string TransferSection
        {
            get { return _TransferSection; }
            set { _TransferSection = value; }
        }

        private string _TransferSectionTime = "";
        /// <summary>
        /// 转科时间
        /// </summary>
        [DefaultValue("")]
        public string TransferSectionTime
        {
            get { return _TransferSectionTime; }
            set { _TransferSectionTime = value; }
        }

        private string _BedNo = null;
        /// <summary>
        /// 床号
        /// </summary>
        [DefaultValue(null)]
        public string BedNo
        {
            get { return _BedNo; }
            set { _BedNo = value; }
        }

        private string _TransferBedNo = "";
        /// <summary>
        /// 转床床号(拼接) +01,3;3,7; 逗号分割当次转床前后床号
        /// </summary>
        [DefaultValue("")]
        public string TransferBedNo
        {
            get { return _TransferBedNo; }
            set { _TransferBedNo = value; }
        }

        private string _TransferBedNoTime = "";
        /// <summary>
        /// 转床时间
        /// </summary>
        [DefaultValue("")]
        public string TransferBedNoTime
        {
            get { return _TransferBedNoTime; }
            set { _TransferBedNoTime = value; }
        }

        private string _InDate = DateTime.Now.ToString();
        /// <summary>
        /// 入院日期
        /// </summary>
        [DefaultValue(null)]
        public string InDate
        {
            get { return _InDate; }
            set { _InDate = value; }
        }

        private string _OutDate = TemperatureDocument.NullDate.ToString();
        /// <summary>
        /// 出院日期
        /// </summary>
        [DefaultValue(null)]
        public string OutDate
        {
            get { return _OutDate; }
            set { _OutDate = value; }
        }

        private FooterMeasureList _FooterMeasures = new FooterMeasureList();
        /// <summary>
        /// 页脚显示的数据
        /// </summary>
        [XmlArrayItem("FooterMeasure", typeof(FooterMeasure))]
        public FooterMeasureList FooterMeasures
        {
            get
            {
                return _FooterMeasures;
            }
            set
            {
                _FooterMeasures = value;
            }
        }

        private MeasureList _Measures = new MeasureList();
        /// <summary>
        /// 测量数据
        /// </summary>
        [XmlArrayItem("Measure", typeof(Measure))]
        public MeasureList Measures
        {
            get
            {
                return _Measures;
            }
            set
            {
                _Measures = value;
            }
        }


        /// <summary>
        /// 更新状态
        /// </summary>
        public void UpdateState()
        {
            this.Measures.Sort(new MeasureComparer());
            this.FooterMeasures.Sort(new CMeasureComparer());

        }

        private string _ShowType = "0";
        /// <summary>
        /// 显示类型
        /// </summary>
        [DefaultValue("0")]
        public string ShowType
        {
            get
            {
                return _ShowType;
            }
            set
            {
                _ShowType = value;
            }
        }



        private int _PageIndex = -1;
        /// <summary>
        /// 从0开始计算的当前页号
        /// </summary>
        [XmlIgnore]
        [Browsable(false)]
        public int PageIndex
        {
            get
            {
                return _PageIndex;
            }
            set
            {
                _PageIndex = value;
                if (_PageIndex < 0 && _PageIndex != -1)
                {
                    _PageIndex = 0;
                }
                if (_PageIndex >= this._NumOfPages)
                {
                    _PageIndex = this.NumOfPages - 1;
                }
            }
        }

        internal int _NumOfPages = 0;
        /// <summary>
        /// 总页数
        /// </summary>
        [Browsable(false)]
        public int NumOfPages
        {
            get
            {
                return _NumOfPages;
            }
        }

        private TemperatureDocumentOptions _Options = new TemperatureDocumentOptions();
        /// <summary>
        /// 文档视图选项
        /// </summary>
        [Browsable(false)]
        [XmlIgnore]
        public TemperatureDocumentOptions Options
        {
            get
            {
                return _Options;
            }
            set
            {
                _Options = value;
                if (_Options == null)
                {
                    _Options = new TemperatureDocumentOptions();
                }
            }
        }

        /// <summary>
        /// 更新文档的总页数
        /// </summary>
        /// <param name="maxDate">输出的数据最大日期</param>
        /// <param name="minDate">输出的数据最小日期</param>
        public void UpdateNumOfPage(out DateTime maxDate, out DateTime minDate)
        {
            // 有数据的时间区间的最大值
            maxDate = NullDate;
            // 有数据的时间区间的最小值
            minDate = NullDate;

            DateTime newDate=DateTime.Now;

            if (this.Measures.Count > 0)
            {
                //计算总页数
                foreach (Measure m in this.Measures)
                {
                    if (m.ValueDate.Date > newDate)
                    {
                        newDate = m.ValueDate.Date;
                    }
                    //if (maxDate == NullDate || maxDate < m.ValueDate.Date )
                    //{
                    //    maxDate = Convert.ToDateTime(OutDate); //m.ValueDate;
                    //}
                    //if (minDate == NullDate || minDate > m.ValueDate.Date )
                    //{
                    //    minDate = Convert.ToDateTime(InDate);
                    //}
                }//foreach
            }
            maxDate = (string.IsNullOrEmpty(OutDate) ||  Convert.ToDateTime(OutDate)==TemperatureDocument.NullDate) ? newDate : Convert.ToDateTime(OutDate);
            minDate = Convert.ToDateTime(InDate);
            TimeSpan valueSpan = maxDate - Convert.ToDateTime(minDate.ToString("yyyy-MM-dd 00:00:00"));
            //此处做了修正(valueSpan.Days+1)
            this._NumOfPages = (int)Math.Ceiling((valueSpan.Days+1)
                / (double)(this.Options.NumOfDaysInOnePage));
            /// (double)(this.Options.NumOfDaysInOnePage-1));
            if (this._NumOfPages == 0)
            {
                this._NumOfPages = 1;
            }
            if (this.PageIndex >= this.NumOfPages)
            {
                this.PageIndex = this.NumOfPages - 1;
            }
            if (this.PageIndex < 0 && this.PageIndex != -1)
            {
                this.PageIndex = 0;
            }
            //重置最大值
            maxDate = string.IsNullOrEmpty(OutDate) ? TemperatureDocument.NullDate : Convert.ToDateTime(OutDate);
            //}
            //else
            //{
            //    maxDate = Convert.ToDateTime(OutDate);// DateTime.Today;
            //    minDate = maxDate.AddDays(-this.Options.NumOfDaysInOnePage);
            //    this._NumOfPages = 1;
            //    this.PageIndex = 0;
            //}
        }


        public bool Load(System.IO.Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            XmlSerializer ser = new XmlSerializer(this.GetType());
            TemperatureDocument doc = (TemperatureDocument)ser.Deserialize(stream);
            if (doc != null)
            {
                this._BedNo = doc._BedNo;
                this._FooterMeasures = doc._FooterMeasures;
                this._PatientID = doc._PatientID;
                this._Measures = doc._Measures;
                this._NumOfPages = doc._NumOfPages;
                this._Options = doc._Options;
                this._PageIndex = doc._PageIndex;
                this._PatientAge = doc._PatientAge;
                this._PatientSex = doc._PatientSex;
                this._Section = doc._Section;
                this._Title = doc._Title;
                this._PatientName = doc.PatientName;
                this._InDate = string.IsNullOrEmpty(doc.InDate) ? DateTime.Now.ToString() : doc.InDate;
                this._OutDate = string.IsNullOrEmpty(doc.OutDate) ? TemperatureDocument.NullDate.ToString() : doc.OutDate;
                this._ShowType = doc._ShowType;
                return true;
            }
            return false;
        }

        public bool LoadXML(XmlReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            XmlSerializer ser = new XmlSerializer(this.GetType());
            TemperatureDocument doc = (TemperatureDocument)ser.Deserialize(reader);
            if (doc != null)
            {
                this._BedNo = doc._BedNo;
                this._FooterMeasures = doc._FooterMeasures;
                this._PatientID = doc._PatientID;
                this._Measures = doc._Measures;
                this._NumOfPages = doc._NumOfPages;
                this._Options = doc._Options;
                this._PageIndex = doc._PageIndex;
                this._PatientAge = doc._PatientAge;
                this._PatientSex = doc._PatientSex;
                this._Section = doc._Section;
                this._Title = doc._Title;
                this._PatientName = doc.PatientName;
                this._InDate = string.IsNullOrEmpty(doc.InDate) ? DateTime.Now.ToString() : doc.InDate;
                this._OutDate = string.IsNullOrEmpty(doc.OutDate) ? TemperatureDocument.NullDate.ToString() : doc.OutDate;
                this._ShowType = doc._ShowType;
                return true;
            }
            return false;
        }

        public void Save(System.IO.Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            XmlSerializer ser = new XmlSerializer(this.GetType());
            ser.Serialize(stream, this);
        }
        public string Result()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
            string xmlString = string.Empty;
            using (MemoryStream ms = new MemoryStream())
            {
                xmlSerializer.Serialize(ms, this);
                xmlString = Encoding.UTF8.GetString(ms.ToArray());
            }
            return xmlString;  
        }

        public TemperatureDocument Clone()
        {
            TemperatureDocument doc = (TemperatureDocument)this.MemberwiseClone();
            if (this._FooterMeasures != null)
            {
                doc._FooterMeasures = this._FooterMeasures.CloneDeeply();
            }
            if (this._Measures != null)
            {
                doc._Measures = this._Measures.CloneDeeply();
            }
            return doc;
        }

        /// <summary>
        /// 表示空日期的字符串
        /// </summary>
        public const string NullDateString = "1980-1-1";
        /// <summary>
        /// 表示空的日期
        /// </summary>
        public static DateTime NullDate = new DateTime(1980, 1, 1);
        /// <summary>
        /// 表示空的数据
        /// </summary>
        public const float NullValue = 0f;

        public static bool IsNullValue(float v)
        {
            return float.IsNaN(v);
            //return float.IsNaN(v) || v == NullValue;
        }

    }
}